package com.bawei.module_visitor

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.base.utils.LoggerUtils
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.module_visitor.adapter.VisitorAdapter
import com.bawei.module_visitor.api.VisitorModel
import com.bawei.module_visitor.databinding.ActivityVisitorBinding
import com.bawei.module_visitor.viewmodel.VisitorViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.Ui.VisitorActivity)
class VisitorActivity : BaseActivity<VisitorViewModel, ActivityVisitorBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_visitor
    override val mViewModel: VisitorViewModel by viewModels()
    var visitorProcess: Int = 0
    private val mVisitorAdapter: VisitorAdapter by lazy {
        VisitorAdapter(
            onStateItemClick = {
                mVisitorAdapter.setSelectedStatePosition(it)
                visitorProcess = it
                mViewModel.getVisitorApply()
            }
        )
    }

    override fun initView() {
        initAdapter()
    }

    override fun initListener() {
        mViewModel.visitorEvent.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {}
                Loading -> {}
                Success -> {
                    when(visitorProcess){
                        0 -> {
                            mVisitorAdapter.setVisitorData(mViewModel.visitorList)
                        }
                        1 -> {
                            mVisitorAdapter.setVisitorData(mViewModel.visitorList.filter { it.visitStatus == "待审核" })
                        }
                        2 -> {
                            mVisitorAdapter.setVisitorData(mViewModel.visitorList.filter { it.visitStatus == "已通过" })
                        }
                        3 -> {
                            mVisitorAdapter.setVisitorData(mViewModel.visitorList.filter { it.visitStatus == "驳回" })
                        }
                        4 -> {
                            mVisitorAdapter.setVisitorData(mViewModel.visitorList.filter { it.visitStatus == "已完成" })
                        }
                    }
                }
            }
        }
        mViewModel.stateList.observe(this){
            mVisitorAdapter.setStatisticsData(it)
        }
        mBinding.visitAdd.setOnClickListener {
            ARouter.getInstance().build(Router.Ui.VisitorAddActivity).navigation()
        }
    }

    override fun initData() {
        mViewModel.getVisitorApply()
        mViewModel.loadStateList()
    }
    private fun initAdapter() {
        mVisitorAdapter.setupLayoutManager(mBinding.visitRec)
        mBinding.visitRec.adapter = mVisitorAdapter
    }

    override fun onResume() {
        super.onResume()
        mViewModel.getVisitorApply()
    }

}